package com.liu.UDPSender;

import java.io.IOException;
import java.net.*;

/**
 * 发出端
 */
public class serverB {
    public static void main(String[] args) throws IOException {
        //创建自己的端口
        DatagramSocket datagramSocket = new DatagramSocket(7778);
        //创建需要发送的数据
        byte[] bytes = "hello，明天吃火锅".getBytes();
        //将数据打包
        DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length, InetAddress.getByName("192.168.56.1"), 7777);
        //发送
        datagramSocket.send(datagramPacket);



        //创建需要数据的容器
         bytes = new byte[1024];
        //将接收的数据打包
        datagramPacket = new DatagramPacket(bytes, bytes.length);
        //接收数据
        datagramSocket.receive(datagramPacket);

        //将实际接收到的数据长度
        int length = datagramPacket.getLength();
        //实际的数据
        byte[] data = datagramPacket.getData();
        //将数据传入S
        String s = new String(data, 0, length);
        //输出
        System.out.println(s);

        datagramSocket.close();
    }
}
